package com.zhai;

import com.zhai.config.RpcConfig;
import com.zhai.scan.DubboScanReflect;
import com.zhai.service.ServiceHandlerPage;
import com.zhai.servlet.InvokePostServlet;
import lombok.Data;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.springframework.beans.factory.InitializingBean;

import java.net.URL;
import java.util.List;

@Data
public class DubboRpcServiceBean implements InitializingBean {

    private List<Class> cacheScanClass;

    /**
     * 配置中心
     */
    private RpcConfig rpcConfig;

    /**
     * 初始化方法
     */
    public void init() {
        try {
            Server server = new Server(4080);

            ContextHandler context = new ContextHandler();
            context.setContextPath("/");
            context.setHandler(new ServiceHandlerPage(rpcConfig));

            //post 请求上下文
            ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
            ServletHolder holder = new ServletHolder(new InvokePostServlet(rpcConfig));
            servletContextHandler.addServlet(holder, "/invoke");

            //添加上下文handler
            ContextHandlerCollection contexts = new ContextHandlerCollection();
            contexts.setHandlers(new Handler[]{servletContextHandler, this.getStaticsHandler(), context});

            server.setHandler(contexts);
            server.start();
            server.join();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 静态资源上下文
     *
     * @return
     */
    public ContextHandler getStaticsHandler() {
        ContextHandler staticsContext = new ContextHandler();
        ServletHolder holder = new ServletHolder(new DefaultServlet());
        URL url = DubboRpcServiceBean.class.getClassLoader().getResource("statics");
        if (url == null) {
            return null;
        }
        String staticsDir = url.toExternalForm();
        holder.setInitParameter("resourceBase", staticsDir);
        holder.setInitParameter("gzip", "false");


        ServletHandler servletHandler = new ServletHandler();
        servletHandler.addServletWithMapping(holder, "");
        servletHandler.addServletWithMapping(holder, "/bootstrap/css/bootstrap-responsive.min.css");
        servletHandler.addServletWithMapping(holder, "/bootstrap/css/bootstrap.min.css");
        servletHandler.addServletWithMapping(holder, "/bootstrap/img/glyphicons-halflings-white.png");
        servletHandler.addServletWithMapping(holder, "/bootstrap/img/glyphicons-halflings.png");
        servletHandler.addServletWithMapping(holder, "/bootstrap/js/bootstrap.min.js");
        servletHandler.addServletWithMapping(holder, "/jquery/jquery-1.7.2.min.js");
        servletHandler.addServletWithMapping(holder, "/jquery/jquery-ui.css");
        servletHandler.addServletWithMapping(holder, "/jquery/jquery-ui.js");
        servletHandler.addServletWithMapping(holder, "/jquery/jquery.easy-confirm-dialog.js");
        servletHandler.addServletWithMapping(holder, "/ztree/img/diy/1_close.png");
        servletHandler.addServletWithMapping(holder, "/ztree/img/diy/1_open.png");
        servletHandler.addServletWithMapping(holder, "/ztree/img/diy/2.png");
        servletHandler.addServletWithMapping(holder, "/ztree/img/diy/3.png");
        servletHandler.addServletWithMapping(holder, "/ztree/img/diy/4.png");
        servletHandler.addServletWithMapping(holder, "/ztree/img/diy/5.png");
        servletHandler.addServletWithMapping(holder, "/ztree/img/diy/6.png");
        servletHandler.addServletWithMapping(holder, "/ztree/img/diy/7.png");
        servletHandler.addServletWithMapping(holder, "/ztree/img/diy/8.png");
        servletHandler.addServletWithMapping(holder, "/ztree/img/diy/9.png");
        servletHandler.addServletWithMapping(holder, "/ztree/img/line_conn.gif");
        servletHandler.addServletWithMapping(holder, "/ztree/img/loading.gif");
        servletHandler.addServletWithMapping(holder, "/ztree/img/zTreeStandard.gif");
        servletHandler.addServletWithMapping(holder, "/ztree/img/zTreeStandard.png");
        servletHandler.addServletWithMapping(holder, "/ztree/jquery.ztree.core-3.3.min.js");
        servletHandler.addServletWithMapping(holder, "/ztree/ztree.css");

        staticsContext.setHandler(servletHandler);
        return staticsContext;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        cacheScanClass = DubboScanReflect.getScanClass(this.rpcConfig.getRpcScanPath());
    }
}
